package com.woniuxy.woniuhis.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.exception.InpatientServiceException;
import com.woniuxy.woniuhis.service.InpatientService;
import com.woniuxy.woniuhis.util.ResponseInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/inpatient")
public class InpatientController {
    @Autowired
    private InpatientService inpatientService;

    @RequestMapping("getInpatientList")
    @ResponseBody
    public ResponseInfo getInpatientList(Inpatient inpatient, Integer pageNum, Integer pageSize) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (inpatient == null) {
                inpatient = new Inpatient();
            }
            inpatient.setOutDate(new Date());
            inpatient.setInDate(new Date());
            PageInfo<Inpatient> pageInfo = inpatientService.findInpatientListByCondition(inpatient, pageNum, pageSize);
            responseInfo = new ResponseInfo(200, pageInfo, "ok");
            responseInfo.setState(200);
            responseInfo.setData(pageInfo);
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }

    @RequestMapping("getInpatientById")
    @ResponseBody
    public ResponseInfo getInpatientById(String inpatientId) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (StringUtils.isEmpty(inpatientId)) {
                responseInfo.setState(500);
                responseInfo.setMsg("服务维护中");
            } else {
                Inpatient inpatientById = inpatientService.findInpatientById(inpatientId);
                responseInfo = new ResponseInfo(200, inpatientById, "ok");
                responseInfo.setState(200);
            }
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }
}
